/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui.tree;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.Serializable;

import javax.swing.*;

import com.pearreview.ui.*;
import com.pearreview.application.Messages;

public class ListCategoryForm extends JPanel {
	private static final long serialVersionUID = 4778624536400538786L;
	private FormLabel formHeading;
	private FormLabel nameLabel;
	private CompletableJTextField name;
	private JButton saveBtn;
	
	private Action saveAction;
	
	public ListCategoryForm(Action saveAction) {
		this.saveAction = saveAction;
		initComponents();
	}
	
	public void setCategory(String text) {
		name.setText(text);
	}
	
	public String getCategory() {
		return name.getText();
	}

	private void initComponents() {

		formHeading = new FormLabel();
		nameLabel = new FormLabel();
		name = new CompletableJTextField();
		saveBtn = new JButton(saveAction);

		//======== this ========
		setOpaque(false);

		//---- formHeading ----
		formHeading.setText(Messages.getString(
				"PearReview.Moderator.aspects.category"));
		formHeading.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		//---- name ----
		name.addKeyListener(new SaveKeyListener());

		//---- nameLabel ----
		nameLabel.setText(Messages.getString(
				"PearReview.Moderator.aspects.name"));
		
		//---- saveBtn ----
		saveBtn.setFont(new Font("Tahoma", Font.PLAIN, 12));

		GroupLayout layout = new GroupLayout(this);
		setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
					.addContainerGap()
					.addGroup(layout.createParallelGroup()
						.addComponent(formHeading, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE)
						.addGroup(layout.createSequentialGroup()
							.addComponent(nameLabel, 
									GroupLayout.PREFERRED_SIZE, 
									100, 
									100)
							.addGap(57)
							.addGroup(layout.createParallelGroup()
									.addGroup(layout.createSequentialGroup()
							.addComponent(name, 
									GroupLayout.DEFAULT_SIZE, 
									200, 
									400))
							.addComponent(saveBtn, 
									GroupLayout.PREFERRED_SIZE, 
									GroupLayout.PREFERRED_SIZE, 
									GroupLayout.PREFERRED_SIZE))))
					.addGap(10))
		);
		layout.setVerticalGroup(
			layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
					.addContainerGap()
					.addComponent(formHeading, GroupLayout.PREFERRED_SIZE, 
							GroupLayout.DEFAULT_SIZE, 
							GroupLayout.PREFERRED_SIZE)
					.addGap(18, 18, 18)
					.addGroup(layout.createParallelGroup(
							GroupLayout.Alignment.BASELINE)
						.addComponent(nameLabel, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE)
						.addComponent(name, GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(
								LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(saveBtn, 
									GroupLayout.PREFERRED_SIZE, 
									GroupLayout.PREFERRED_SIZE, 
									GroupLayout.PREFERRED_SIZE))
		);
	}
	
	private class SaveKeyListener implements KeyListener, Serializable {
		private static final long serialVersionUID = 4805036279156748029L;
		@Override
		public void keyPressed(KeyEvent e) {
			if(e.getKeyCode() == KeyEvent.VK_ENTER) {
				saveBtn.doClick();
			}			
		}
		@Override
		public void keyReleased(KeyEvent e) {
			// do nothing			
		}
		@Override
		public void keyTyped(KeyEvent e) {
			// do nothing			
		}		
	}
}